﻿using System.Collections;
using System.Collections.Generic;

namespace Iot.TimingDatabase.Models;

/// <summary>
///     表示IoT数据库列数据的记录类型，实现了IReadOnlyList<object />接口
/// </summary>
/// <param name="Info">列信息</param>
/// <param name="Data">数据的只读列表</param>
public record IotDbColumnData(ColumnInfo Info, IReadOnlyList<object?> Data) : IReadOnlyList<object?>
{
    /// <summary>
    ///     获取数据行数
    /// </summary>
    public int Count => Data.Count;

    /// <summary>
    ///     实现IEnumerable<object />的GetEnumerator方法
    /// </summary>
    /// <returns>枚举器</returns>
    public IEnumerator<object?> GetEnumerator()
    {
        for (var i = 0; i < Count; i++)
        {
            yield return this[i];
        }
    }

    /// <summary>
    ///     实现IEnumerable的GetEnumerator方法
    /// </summary>
    /// <returns>枚举器</returns>
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    /// <summary>
    ///     索引器，获取指定索引的数据
    /// </summary>
    /// <param name="index">数据的索引</param>
    /// <returns>对应索引的数据，如果索引无效则返回null</returns>
    public object? this[int index] => Count > index ? Data[index] : null;
}